#include "OLTSPInstanceMetric.h"
#include "Request.h"
#include <list>
#include <algorithm>
#include <iostream>

void OLTSPInstance::add_intermediate_request(Request& start, Request& end, float distance_from_start, float creation_time) {
	float total_distance = start.get_distance(end);
	float distance_from_end = total_distance - distance_from_start;
	vector<float> new_distances(requests.size()+1);
	new_distances[requests.size()] = 0.0;

	new_distances[start.id] = distance_from_start;
	new_distances[end.id] = distance_from_end;
	for (int i = 0; i< requests.size(); i++) {
		if (i!=start.id && i!=end.id) {
			float i_distance_by_start = distance_from_start + start.distances[i];
			float i_distance_by_end = distance_from_end + end.distances[i];
			if (i_distance_by_start < i_distance_by_end)
				new_distances[i] = i_distance_by_start;
			else
				new_distances[i] = i_distance_by_end;
		}

	}

	//
	list<Request>::iterator itr = requests.begin();
	for (; itr!= requests.end(); ++itr){
		itr->distances.push_back(new_distances[itr->id]);
	}




	Request new_request(requests.size(), new_distances, creation_time);
	requests.push_back(new_request);

}

OLTSPInstanceMetric::OLTSPInstanceMetric(char* filename) : OLTSPInstance(filename) {};
